﻿using Microsoft.Extensions.Logging;
using Simple.Framework.Configuration;
using Simple.Framework.Ioc;
using Simple.Framework.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 SimpleNet
 * 接口名称 SimpleClient
 * 开发人员：ADMIN
 * 创建时间：2023/12/29 10:28:06
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace SimpleNet.Client
{
    public class SimpleClient: ISimpleClient,IDisposable
	{
        private IConnection connection;
		private ClientConfiguration cfg;

		private ILogger<SimpleClient> logger;
        public SimpleClient(IConnection connection,IConfiguration configuration,ILogger<SimpleClient> logger)
        {
			this.logger = logger;	
            this.connection = connection;
			cfg = configuration.GetValue<ClientConfiguration>("client");
			CreateConnection();

		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="encoderType"></param>
        public void SendMessage(string message,EncoderType encoderType= EncoderType.UTF_8)
        {
            switch (encoderType)
            {
                case EncoderType.UTF_8:
					connection.Context.SendUTF8(message);
					break;
                case EncoderType.ASCII:
					connection.Context.SendASCII(message);
					break;
                default:
                    break;
            }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="message"></param>
		/// <param name="socketFlags"></param>
		/// <param name="encoderType"></param>
		/// <param name="cancellation"></param>
		/// <returns></returns>
		public async Task SendMessageAsync(string message, SocketFlags socketFlags , EncoderType encoderType = EncoderType.UTF_8, CancellationToken cancellation=default)
		{
			switch (encoderType)
			{
				case EncoderType.UTF_8:
					await connection.Context.SendUTF8Async(message,socketFlags, cancellation);
					break;
				case EncoderType.ASCII:
					await connection.Context.SendASCIIAsync(message, socketFlags, cancellation);
					break;
				default:
					break;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="buffer"></param>
		public void Send(byte[] buffer)
		{
			this.connection.Context.Send(buffer);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="buffer"></param>
		/// <param name="socketFlags"></param>
		/// <param name="cancellationToken"></param>
		/// <returns></returns>
		public async Task SendAsync(byte[] buffer,SocketFlags socketFlags=default,CancellationToken cancellationToken=default)
		{
			await this.connection.Context.SendAsync(buffer, socketFlags,cancellationToken);
		}


		/// <summary>
		/// 
		/// </summary>
		private void CreateConnection()
		{
			try
			{
				Socket socket = new Socket(cfg.SocketType, cfg.ProtocolType);
				IPEndPoint endPoint = new IPEndPoint(cfg.GetServer(), cfg.ServerPort);
				socket.Connect(endPoint);
				connection.SetSocket(socket);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.Message);
				logger.LogError(ex.StackTrace);
				Dispose();
			}
			finally
			{
			}
		}

		public void Dispose()
		{
			connection.Dispose();
		}
	}
}
